
package com.skcc.cop.mrtaxi4driver.biz;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.skcc.cop.mrtaxi4driver.BaseActivity;
import com.skcc.cop.mrtaxi4driver.R;
import com.skcc.cop.mrtaxi4driver.R.color;
import com.skcc.cop.mrtaxi4driver.biz.HistoryActivity.OnLoadHistoryListener;
import com.skcc.cop.mrtaxi4driver.http.model.CallList;
import com.skcc.cop.mrtaxi4driver.util.Variables;


public class HistoryTab1 extends BaseActivity {

	// ===========================================================
    // Constants
    // ===========================================================
	private static final String TAG = HistoryTab1.class.getSimpleName();
	
	
	// ===========================================================
    // Fields
    // ===========================================================
    
	
    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		FragmentManager fm = getSupportFragmentManager();
		// Create the list fragment and add it as our sole content.
		if (fm.findFragmentById(android.R.id.content) == null) {
			HistoryTab1Fragment historyTab1Fragment = new HistoryTab1Fragment();
			fm.beginTransaction().add(android.R.id.content, historyTab1Fragment).commit();
		}
	}
	
	
	// ===========================================================
    // Methods
    // ===========================================================
    
	
    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
	public static class HistoryTab1Fragment extends Fragment 
		implements OnLoadHistoryListener {

		private ListView mCallListListview;
		private CallListAdapter mAdapter;
		private List<CallList> callHistoryList;

		@Override
		public void onAttach(Activity activity)
		{
			Log.i(TAG, "HistoryTab1Fragment ======== onAttach");
			
			super.onAttach(activity);
		}
		
		/**
		 * When creating, retrieve this instance's number from its arguments.
		 */
		@Override
		public void onCreate(Bundle savedInstanceState) {
			Log.i(TAG, "HistoryTab1Fragment ======== onCreate");
			
			super.onCreate(savedInstanceState);
		}

		/**
		 * The Fragment's UI is just a simple text view showing its instance number.
		 */
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.fragment_list, container, false);
			mCallListListview = (ListView) v.findViewById(R.id.history_calllist_listview);
			
			callHistoryList = new ArrayList<CallList>();
			// Create an empty adapter we will use to display the loaded data.
			mAdapter = new CallListAdapter(((BaseActivity)getActivity()), R.layout.view_callhistory_list_item, callHistoryList);
			mCallListListview.setAdapter(mAdapter);
			
			return v;
		}

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			Log.i(TAG, "HistoryTab1Fragment ======== onActivityCreated");
			
			((HistoryActivity) getActivity()).setOnLoadHistoryListener1(this);
			
			((HistoryActivity) getActivity()).getCallList(Variables.BOOLEAN_VALUE_Y);
		}

		@Override
		public void onStart() {
			Log.i(TAG, "HistoryTab1Fragment ======== onStart");
			super.onStart();
		}

		@Override
		public void onStop() {
			Log.i(TAG, "HistoryTab1Fragment ======== onStop");
			super.onStop();
		}

		@Override
		public void onDestroy() {
			Log.i(TAG, "HistoryTab1Fragment ======== onDestroy");
			super.onDestroy();
		}

		@Override
		public void onDetach() {
			Log.i(TAG, "HistoryTab1Fragment ======== onDetach");
			super.onDetach();
		}

		@Override
		public void onLoad(List<CallList> historyList) {
			callHistoryList.clear();
			
			callHistoryList.addAll(historyList);
			mAdapter.notifyDataSetChanged();
		}

	}
	
	/**
	 * 리스트 UI 의 데이터 Control 부분을 담당하는 클래스
	 */
	private static class CallListAdapter extends ArrayAdapter<CallList>
	{
		private LayoutInflater mInflater;
		private int resource;
		private Context context;
		
		public CallListAdapter(Context context, int resource, List<CallList> historyList)
		{
			super(context, resource, historyList);
			
			this.context = context;
			this.resource = resource;
			this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent)
		{
			// A ViewHolder keeps references to children views to avoid
			// unneccessary calls to findViewById() on each row.
			ViewHolder holder;
			
			// When convertView is not null, we can reuse it directly, there is no need to reinflate it.
			// We only inflate a new View when the convertView supplied by ListView is null
			if (convertView == null) {
				convertView = mInflater.inflate(this.resource, parent, false);
				
				// Creates a ViewHolder and store references to the 3 children
				// views we want to bind data to.
				holder = new ViewHolder();
				holder.layout = (LinearLayout) convertView.findViewById(R.id.history_calllist_item_layout);
				holder.destination = (TextView) convertView.findViewById(R.id.history_calllist_destination);
				holder.customerNumber = (TextView) convertView.findViewById(R.id.history_calllist_number_of_customer);
				holder.phoneNumber = (Button) convertView.findViewById(R.id.history_phonenumber);
				holder.shortcut = (Button) convertView.findViewById(R.id.history_shortcut);
				
				convertView.setTag(holder);
			} else {
				// Get the ViewHolder back to get fast access to the TextView
				holder = (ViewHolder) convertView.getTag();
			}
			
			final CallList item = getItem(position);
			
			holder.destination.setText(item.getPsgAddr1());
			holder.customerNumber.setText(item.getPsgNum());
			
			holder.phoneNumber.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					
					Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + item.getPhoneNumber()));
					context.startActivity(intent);
				}
				
			});
			
			holder.shortcut.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO: item
					// 1. item.getLatitude(), item.getLongitude() 를 사용하여 T-map 에 최단거리 표시
					// 2. 기사의 현재 위치를 가져오는 기능 필요.
					Intent intent = new Intent(context, DriverMapActivity.class);
					
					//intent.addFlags();
					Log.i(TAG, ""+item.getLatitude());
					Log.i(TAG, ""+item.getLongitude());

					intent.putExtra("latitude", item.getLatitude());
					intent.putExtra("longitude", item.getLongitude());
					/*
					intent.putExtra("myLocX", myLocX);
					intent.putExtra("myLocY", myLocY);
					intent.putExtra("isPath", isPath);
					*/
					context.startActivity(intent);
					
				}
				
			});
			
			if ((position%2) == 0) {
				holder.layout.setBackgroundResource(R.color.white);
			} else {
				holder.layout.setBackgroundResource(color.lightgray);
			}
			
			return convertView;
		}
		
		class ViewHolder {
			LinearLayout layout;
			TextView destination;
			TextView customerNumber;
			Button phoneNumber;
			Button shortcut;
		}
	}
	
}
